package com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.math.BigDecimal;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.customerTwo.EMP.price_setting.service.ITPriceSettingService;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.service.ITWaterCostRecordsDayService;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.dto.TWaterCostRecordsMonthDto;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.dto.SaveTWaterCostRecordsMonthFormDataDto;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.vo.TWaterCostRecordsMonthVo;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.vo.TWaterCostRecordsMonthListVo;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.dto.TWaterCostRecordsMonthListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.entity.TWaterCostRecordsMonth;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.vo.TWaterCostRecordsMonthVo;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.service.ITWaterCostRecordsMonthService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2023-09-05
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/t_water_cost_records_month")
@Api(value = "月度水费记录", tags = "月度水费记录接口")
public class TWaterCostRecordsMonthController {


	private final ITWaterCostRecordsMonthService tWaterCostRecordsMonthService;
	//电费单价设置
	private final ITPriceSettingService itPriceSettingService;
	//每日水费记录
	private final ITWaterCostRecordsDayService itWaterCostRecordsDayService;


	/**
	 * 详情 [{fieldName=department, lazyType=department}]
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tWaterCostRecordsMonth")
	@SaCheckPermission(value = {"t_water_cost_records_month:view", "t_water_cost_records_month:modify"}, mode = SaMode.OR)
	public Response<TWaterCostRecordsMonthVo> getFormData(@PathVariable String id) {
		// 主表数据
		TWaterCostRecordsMonth tWaterCostRecordsMonth = tWaterCostRecordsMonthService.getById(id);
		TWaterCostRecordsMonthVo tWaterCostRecordsMonthVo = BeanUtil.copy(tWaterCostRecordsMonth, TWaterCostRecordsMonthVo.class);
		tWaterCostRecordsMonthVo.setDepartment_Name(DataTransUtil.getDepartmentNameById(tWaterCostRecordsMonthVo.getDepartment()));
		return Response.ok(tWaterCostRecordsMonthVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tWaterCostRecordsMonth")
		public Response<PageOutput<TWaterCostRecordsMonthListVo>> getPageList(TWaterCostRecordsMonthListDto listDto) {
		IPage<TWaterCostRecordsMonth> page = tWaterCostRecordsMonthService.getPageList(listDto);
		List<TWaterCostRecordsMonthListVo> records = BeanUtil.copyList(page.getRecords(), TWaterCostRecordsMonthListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tWaterCostRecordsMonth")
	@SaCheckPermission("t_water_cost_records_month:add")
	public Response save(@RequestBody SaveTWaterCostRecordsMonthFormDataDto formDto) {
		TWaterCostRecordsMonthDto tWaterCostRecordsMonthDto = formDto.getTWaterCostRecordsMonthDto();
		//处理起始日期
		int month = Integer.parseInt(tWaterCostRecordsMonthDto.getMonth())+1;
		String start = tWaterCostRecordsMonthDto.getYear()+"-"+tWaterCostRecordsMonthDto.getMonth()+"-01";
		String end = "";
		if(month == 13){
			int year = Integer.parseInt(tWaterCostRecordsMonthDto.getYear())+1;
			end = year+"-01-01";
		}else {
			end = tWaterCostRecordsMonthDto.getYear()+"-"+(month>9?month:"0"+month)+"-01";
		}
		//删除历史数据
		tWaterCostRecordsMonthService.deleteWaterDate(start,end);
		tWaterCostRecordsMonthService.deleteWaterCostRecordsDay(start,end);
		tWaterCostRecordsMonthService.deleteWaterCostRecordsMonth(tWaterCostRecordsMonthDto.getYear(),tWaterCostRecordsMonthDto.getMonth());
		//数据写入中转表(原始数据变成根据房间号、日期汇总每天最小用水量(当天最小-昨天最小=昨天整天用水量))
		int count = tWaterCostRecordsMonthService.addTransitionData(start,end);
		//中转表数据汇总（从中转表最小用水量计算当天用水量）
		if(count == 0){
			log.warn("TWaterCostRecordsMonthController addTransitionData no data. date {}-{}",start,end);
			Response.ok();
		}
		count = tWaterCostRecordsMonthService.addSumData(start,end);
		if(count == 0){
			log.warn("TWaterCostRecordsMonthController addSumData no data. date {}-{}",start,end);
			Response.ok();
		}
		//取冷水费单价、热水费单价
		BigDecimal coldWaterPrice = itPriceSettingService.getColdWaterPrice();
		BigDecimal heatWaterPrice = itPriceSettingService.getHeatWaterPrice();
		//计算每人每天用水量、费用
		count = itWaterCostRecordsDayService.addDayRecordsData(start,end,coldWaterPrice,heatWaterPrice);
		if(count == 0){
			log.warn("TWaterCostRecordsMonthController addDayRecordsData no data. date {}-{}",start,end);
			Response.ok();
		}else {
			log.info("TWaterCostRecordsMonthController save day data success. total {}",count);
		}
		//查询需要分摊水费的数据
		List<String> ids = itWaterCostRecordsDayService.selectProportionIdList(start,end);
		if(CollectionUtil.isNotEmpty(ids)){
			itWaterCostRecordsDayService.updateProportionData(ids);
		}
		//处理月度水费记录
		count = tWaterCostRecordsMonthService.insertDataByDay(start,end);
		if(count == 0){
			log.warn("TWaterCostRecordsMonthController insertDataByDay no data. date {}-{}",start,end);
		}else{
			log.info("TWaterCostRecordsMonthController save month data success. total {}",count);
		}
		return Response.ok();
	}


}
